"""
通过qq邮件发送到服务器，自动转发到对方收件箱服务器
"""
import smtplib  #用于发送邮件
from email.mime.text import MIMEText  #构造邮件文本

#  设置邮件参数
host = "smtp.qq.com"
port = 465
username = "3155472712@qq.com"  # 写自己的！！！！！
with open("授权码.txt","r",encoding="utf-8") as f:
    pwd = f.read().strip()
names,scores,receivers = [],[],[]  # 可以写多个，会同时给多个人发同一封邮件
with open("成绩.txt","r",encoding="utf-8") as f:
    for line in f:
        info = line.split(" ")  # 如["张三","40","张三邮箱"]
        names.append(info[0])
        scores.append(info[1])
        receivers.append(info[2])

# 设置邮件的主题和正文
subject = "补考通知"
content = ""

# 先登录
link = smtplib.SMTP_SSL(host,port)  # 连接腾讯发邮件服务器
link.login(username,pwd)  # 登录

# 循环构造邮件对象 并发送
for name,score,receiver in zip(names,scores,receivers):
    if int(score) < 60:
        content = f'你好,{name}:<br/> 你的分数是<font size="8" color="red">{score}</font>,请注意后续补考通知！'
        msg = MIMEText(content,"html","utf-8")  # 第二个参数 plain代表纯文本 html代表支持网页标签
        msg["Subject"] = subject
        msg["From"] = "TeacherChen<"+username+">"
        msg["To"] = receiver
        link.send_message(msg)  # 发送

#  最后退出
link.quit()  # 退出
